#include "bitmap.h"

void BitMap::LoadBitmapFile (HWND hwnd, char* filename,HDC hdc)
{
	B_BITMAP* Header			  =(B_BITMAP*) malloc (sizeof(B_BITMAP));
	FILE* infile				  = NULL;
	unsigned int ImagePixelAmount = 0;
	char *rawData;
	long fileSize;

	fopen_s(&infile, filename, "rb");

	if(infile == NULL)
	{
		MessageBox(NULL, "Could not Load image Error!", "Error" , MB_OK);
		DestroyWindow(hwnd);
		return;
	}

	//read bitmap fileheader................................

	fseek(infile, 0, SEEK_SET);
	fread(&Header->fileHeader, sizeof(B_FILEHEADER), 1 , infile);

	//check bitmap file

	if(Header->fileHeader.bfType != 19778)
	{
		MessageBox(NULL, "Could not format file BMP!", "Error" , MB_OK);
		DestroyWindow(hwnd);
		fclose(infile);
	}

	//read bitmap infoheader..................................
	fread(&Header->infoHeader, sizeof(B_INFOHEADER), 1, infile);

	ImagePixelAmount = (Header->infoHeader.biHeight) * (Header->infoHeader.biWidth);

	// create space in memory and read in pixel data from image

	Header->IMAGEDATA = (B_RGB*) calloc(sizeof(B_RGB),ImagePixelAmount);

	fseek (infile, Header->fileHeader.bfOffBits, SEEK_SET);

	fread (Header ->IMAGEDATA, ImagePixelAmount * sizeof(B_RGB) , 1 , infile);

	///////////////////////////////////////////////////////////////////////////////////////////////
	//read file 8-bit////////////////////////////

	fseek(infile, 0, SEEK_END);

    fileSize = ftell(infile);
    fseek(infile, 0, SEEK_SET);

    rawData = (char*)malloc( fileSize * sizeof(char) );
    fread(rawData, sizeof(char), fileSize, infile);

	fclose(infile);

	////////////////////////////////////////////////////////////////////////////////////
	// show pixel data 
	if(Header->infoHeader.biBitCount==24)
	{
		COLORREF color;
		int z = Header->infoHeader.biHeight;
		unsigned int r,g,b;
		for(int y = 0 ; y < Header->infoHeader.biHeight ; y++)
		{
			for(int x = 0 ; x < Header->infoHeader.biWidth ; x++)
			{
				r = (unsigned int) Header->IMAGEDATA [ y * Header->infoHeader.biWidth + x].rRed;
				g = (unsigned int) Header->IMAGEDATA [ y * Header->infoHeader.biWidth + x].gGreen;
				b = (unsigned int) Header->IMAGEDATA [ y * Header->infoHeader.biWidth + x].bBlue;
				color = RGB(r,g,b);
				SetPixel(hdc,x,z, color);
			}
			z--;
		}
	}
	 
	else if(Header->infoHeader.biBitCount==8)
	{
		
		BGRA* colour_table_offset;
		colour_table_offset = (BGRA*) (rawData + (sizeof(B_FILEHEADER) + sizeof(B_INFOHEADER)));

		int bytesPerRow = Header->infoHeader.biWidth + (Header->infoHeader.biWidth%2);    // rows must be a multiple of 2 bytes

		int y, x;
		int z = Header->infoHeader.biHeight;
		for (y=0; y<Header->infoHeader.biHeight; y++)
		{
			UINT8* curRow;
			curRow = (UINT8*) (Header->fileHeader.bfOffBits + (y*bytesPerRow) + rawData);
			for (x=0; x<Header->infoHeader.biWidth; x++)
			{
				UINT32 palIndex;
				palIndex = curRow[x];
				BGRA curCol = colour_table_offset[palIndex];

				UINT8 r, g, b;
				b = (curCol>>0) & 0xFF;
				g = (curCol>>8) & 0xFF;
				r = (curCol>>16) & 0xFF;
				SetPixel(hdc, x,z, RGB(r,g,b) );
			}
			z--;
		}
	
	}

	else
	{
		MessageBox(NULL, "Format Error, are you sure 24-bit or 8-bit bmp?", "Error", MB_OK);
		DestroyWindow(hwnd);
		return;
	}
	free(Header);
	free(rawData);
}


void BitMap::dispBmp(char *filename, HDC hdc)
{
    FILE *fp;
    long fileSize;
    char *rawData;

    fp = fopen(filename, "rb");
    fseek(fp, 0, SEEK_END);
    fileSize = ftell(fp);
    fseek(fp, 0, SEEK_SET);

    rawData = (char*)malloc( fileSize * sizeof(char) );
    fread(rawData, sizeof(char), fileSize, fp);

    B_BITMAP *bmHdr;
    bmHdr = (B_BITMAP*)rawData;

    BGRA *colour_table_offset;
	colour_table_offset = (BGRA*) (rawData + (sizeof(B_FILEHEADER) + sizeof(B_INFOHEADER)));

	int bytesPerRow = bmHdr->infoHeader.biWidth + (bmHdr->infoHeader.biWidth%2);    // rows must be a multiple of 2 bytes

    int y, x;
	int z = bmHdr->infoHeader.biHeight;
    for (y=0; y<bmHdr->infoHeader.biHeight; y++)
    {
		UINT8 *curRow;
		curRow = (UINT8*) (bmHdr->fileHeader.bfOffBits + (y*bytesPerRow) + rawData);
        for (x=0; x<bmHdr->infoHeader.biWidth; x++)
        {
            UINT32 palIndex;
            palIndex = curRow[x];
            BGRA curCol = colour_table_offset[palIndex];

            UINT8 r, g, b;
            b = (curCol>>0) & 0x10;
            g = (curCol>>4) & 0x10;
            r = (curCol>>8) & 0x10;
            SetPixel(hdc, x,z, RGB(r,g,b) );
        }
		z--;
    }
    free(rawData);
    fclose(fp);
}